//
//  WPAccount.m
//  01 微博
//
//  Created by zhoup on 16/5/20.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "WPAccount.h"

@implementation WPAccount

// 将模型写入文件时调用
- (void)encodeWithCoder:(NSCoder *)encoder
{
    // 如果想简化写入文件和从该文件中读取的代码, 可以用运行时完成
    [encoder encodeObject:self.access_token forKey:@"access_token"];
    [encoder encodeObject:self.uid forKey:@"uid"];
}

// 从文件中读取模型时调用
- (instancetype)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        self.access_token = [decoder decodeObjectForKey:@"access_token"];
        
        self.uid = [decoder decodeObjectForKey:@"uid"];
    }
    return self;
}


- (BOOL)save
{
    // 1.获取沙盒路径
    NSString *accountPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    accountPath =[accountPath stringByAppendingPathComponent:@"account.archiver"];
    
    // 2.将自己存储起来
    return [NSKeyedArchiver archiveRootObject:self toFile:accountPath];
}

+ (instancetype)accountFromSandbox
{

    // 1.获取沙盒路径
    NSString *accountPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    accountPath =[accountPath stringByAppendingPathComponent:@"account.archiver"];
    
    // 2.取出存储的模型对象
    WPAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath];
    
        // 3.返回模型对象
    return account;
    
}

+(void)saveLoginStatus
{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults] ;
    [userDefault setObject:@"logined" forKey:@"kLogined"];
    [userDefault synchronize];
}

+(BOOL)isLogin
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults] ;
    NSString *loginstatus =  [userDefault objectForKey:@"kLogined"];
    
    return [loginstatus isEqualToString:@"logined"];
}

@end
